package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.annotation.Login;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {
    @Resource
    private OrderInfoService orderInfoService;

    @PostMapping("trade")
    @Login
    public Result<OrderInfoVo> trade(@RequestBody TradeVo tradeVo) {
        OrderInfoVo orderInfoVo = this.orderInfoService.trade(tradeVo);
        return Result.ok(orderInfoVo);
    }

    @PostMapping("submitOrder")
    @Login
    public Result<Map<String, Object>> submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
        this.orderInfoService.submitOrder(orderInfoVo);
        Map<String, Object> map = new HashMap<>();
        map.put("orderNo", orderInfoVo.getTradeNo());
        return Result.ok(map);
    }

    @GetMapping("getOrderInfo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
        OrderInfo orderInfo = this.orderInfoService.getOrderInfo(orderNo);
        return Result.ok(orderInfo);
    }

    @Operation(summary = "分页查询订单列表")
    @GetMapping("findUserPage/{page}/{limit}")
    @Login
    public Result<Page<OrderInfo>> findUserPage(@PathVariable int page,@PathVariable int limit) {
        Page<OrderInfo> orderInfoPage = this.orderInfoService.findUserPage(page, limit);
        return Result.ok(orderInfoPage);
    }
}

